/*     */ package net.sourceforge.appgen.generator;
/*     */ 
/*     */ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.Collections;
import java.util.Properties;

import net.sourceforge.appgen.Activator;
import net.sourceforge.appgen.model.AttachFile;
import net.sourceforge.appgen.model.AttachFilePersister;
import net.sourceforge.appgen.model.AttachFilePropertyEditor;
import net.sourceforge.appgen.model.BaseCriteria;
import net.sourceforge.appgen.model.BaseService;
import net.sourceforge.appgen.model.Controller;
import net.sourceforge.appgen.model.Criteria;
import net.sourceforge.appgen.model.Dao;
import net.sourceforge.appgen.model.DefaultMethodInvocationLogger;
import net.sourceforge.appgen.model.Entity;
import net.sourceforge.appgen.model.FilenameGenerator;
import net.sourceforge.appgen.model.FormController;
import net.sourceforge.appgen.model.GenerationInformation;
import net.sourceforge.appgen.model.Jstl;
import net.sourceforge.appgen.model.MethodInvocationInfoInterceptor;
import net.sourceforge.appgen.model.MethodInvocationLogger;
import net.sourceforge.appgen.model.MethodInvocationLoggingAdvice;
import net.sourceforge.appgen.model.Paging;
import net.sourceforge.appgen.model.Service;
import net.sourceforge.appgen.model.SqlMap;
import net.sourceforge.appgen.model.UUIDFilenameGenerator;
import net.sourceforge.appgen.model.Validator;
import net.sourceforge.appgen.model.WebXml;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.osgi.framework.Bundle;
/*     */ 
/*     */ public abstract class FileGenerator
/*     */ {
/*     */   protected Properties properties;
/*     */   protected File templateDir;
/*     */   protected File outputDir;
/*     */   protected String packageName;
/*     */ 
/*     */   public FileGenerator(GenerationInformation generationInformation)
/*     */   {
/*  73 */     this.properties = new Properties();
/*  74 */     this.templateDir = generationInformation.getTemplateDir();
/*  75 */     this.outputDir = generationInformation.getOutputDir();
/*  76 */     this.packageName = generationInformation.getPackageName();
/*     */ 
/*  78 */     if (!generationInformation.isSpecifyTemplateDir()) {
/*  79 */       Bundle bundle = Activator.getDefault().getBundle();
/*  80 */       Path path = new Path("template");
/*  81 */       URL url = FileLocator.find(bundle, path, Collections.EMPTY_MAP);
/*  82 */       URL fileUrl = null;
/*     */       try
/*     */       {
/*  85 */         fileUrl = FileLocator.toFileURL(url);
/*     */       } catch (IOException e) {
/*  87 */         e.printStackTrace();
/*     */       }
/*     */ 
/*  90 */       this.templateDir = new File(fileUrl.getPath());
/*     */     }
/*     */ 
/*  93 */     this.properties.setProperty("file.resource.loader.path", this.templateDir.getPath());
/*     */   }
/*     */ 
/*     */   public boolean existFile(Entity entity) {
/*  97 */     return getFile(entity).exists();
/*     */   }
/*     */ 
/*     */   public void generateDirectory() {
/* 101 */     if (!getDirectory().exists())
/* 102 */       getDirectory().mkdirs();
/*     */   }
/*     */ 
/*     */   public File generate(Entity entity) throws Exception
/*     */   {
/* 107 */     return generateFile(entity, getTemplate(), getFile(entity)); } 
/*     */   public abstract File getFile(Entity paramEntity);
/*     */ 
/*     */   public abstract File getDirectory();
/*     */ 
/*     */   public abstract String getTemplate();
/*     */ 
/* 117 */   public File generateFile(Entity entity, String template, File file) throws Exception { Velocity.init(this.properties);
/*     */ 
/* 119 */     Template modelTemplate = Velocity.getTemplate(template,"UTF-8");
/*     */ 
/* 121 */     VelocityContext context = new VelocityContext();
/*     */ 
/* 123 */     context.put("numberFormat", "###,###");
/* 125 */     context.put("baseService", new BaseService(this.packageName));
/* 126 */     context.put("baseCriteria", new BaseCriteria(this.packageName));
/* 127 */     context.put("paging", new Paging(this.packageName));
/*     */ 
/* 129 */     context.put("attachFile", new AttachFile(this.packageName));
/* 130 */     context.put("attachFilePersister", new AttachFilePersister(this.packageName));
/* 131 */     context.put("attachFilePropertyEditor", new AttachFilePropertyEditor(this.packageName));
/* 132 */     context.put("filenameGenerator", new FilenameGenerator(this.packageName));
/* 133 */     context.put("uUIDFilenameGenerator", new UUIDFilenameGenerator(this.packageName));
/*     */ 
/* 135 */     context.put("defaultMethodInvocationLogger", new DefaultMethodInvocationLogger(this.packageName));
/* 136 */     context.put("methodInvocationInfoInterceptor", new MethodInvocationInfoInterceptor(this.packageName));
/* 137 */     context.put("methodInvocationLogger", new MethodInvocationLogger(this.packageName));
/* 138 */     context.put("methodInvocationLoggingAdvice", new MethodInvocationLoggingAdvice(this.packageName));
/*     */ 
/* 140 */     context.put("entity", entity);
/* 141 */     context.put("criteria", new Criteria(entity));
/* 142 */     context.put("validator", new Validator(entity));
/* 143 */     context.put("dao", new Dao(entity));
/* 144 */     context.put("sqlMap", new SqlMap(entity));
/* 145 */     context.put("service", new Service(entity));
/* 146 */     context.put("controller", new Controller(entity));
/* 147 */     context.put("formController", new FormController(entity));
/*     */ 
/* 149 */     context.put("webXml", new WebXml(entity));
/* 150 */     context.put("jstl", new Jstl(entity));
/*     */ BufferedWriter writer=null;
/*     */     try
/*     */     {
				FileOutputStream fos = new FileOutputStream(file);  
/*     */ 		 writer = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
/* 157 */       modelTemplate.merge(context, writer);
/*     */     } catch (Exception e) {
/* 159 */       throw e;
/*     */     } finally {
/* 161 */       if (writer != null)
/*     */         try {
/* 163 */           writer.close();
/*     */         }
/*     */         catch (Exception localException1)
/*     */         {
/*     */         }
/*     */     }
/* 169 */     return file;
/*     */   }
/*     */ }

/* Location:           C:\Users\lzl6066\Downloads\net.sourceforge.appgen_1.2.0\plugins\net.sourceforge.appgen_1.2.0.201002031718\
 * Qualified Name:     net.sourceforge.appgen.generator.FileGenerator
 * JD-Core Version:    0.6.0
 */